package com.shengdong.water.code.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.withStyledAttributes
import com.shengdong.water.R

class DegreePointSecondView @JvmOverloads  constructor(

    context: Context,

    attrs: AttributeSet? = null,

    defStyleAttr: Int = 0

) : LinearLayout(context, attrs, defStyleAttr){

    companion object {

        const val MODE_DEGREE = 0           // 度数
        const val MODE_DEGREE_POINT = 1     // 度数+分
        const val MODE_DEGREE_POINT_SECOND = 2   // 度分秒

    }


    private val degreeEditText: AppCompatEditText

    private val pointEditText: AppCompatEditText

    private val secondEditText: AppCompatEditText

    private val degreeLayout: LinearLayout

    private val pointLayout: LinearLayout

    private val secondLayout: LinearLayout

    // 属性
    private var mode: Int = MODE_DEGREE

    private var onDegreeChangeListener: ((String) -> Unit)? = null
    private var onPointChangeListener: ((String) -> Unit)? = null
    private var onSecondChangeListener: ((String) -> Unit)? = null


    init {
        // 加载布局
        LayoutInflater.from(context).inflate(R.layout.layout_degree_point_second, this, true)
        // 获取控件引用
        degreeEditText = findViewById(R.id.et_degree)
        pointEditText = findViewById(R.id.et_point)
        secondEditText = findViewById(R.id.et_second)
        degreeLayout = findViewById(R.id.layout_degree)
        pointLayout = findViewById(R.id.layout_point)
        secondLayout = findViewById(R.id.layout_second)
        // 设置默认高度
//        val defaultHeight = resources.getDimensionPixelSize(R.dimen.dp50)
//
//        layoutParams = androidx.constraintlayout.widget.ConstraintLayout.LayoutParams(
//            androidx.constraintlayout.widget.ConstraintLayout.LayoutParams.MATCH_PARENT,
//            defaultHeight
//        )

        // 读取自定义属性
        context.withStyledAttributes(attrs, R.styleable.DegreePointSecondView) {
            initAttributes(this)

        }

    }


    private fun initAttributes(typedArray: TypedArray) {

        // 读取属性

        mode = typedArray.getInt(R.styleable.DegreePointSecondView_dps_mode, MODE_DEGREE)

        // 根据模式设置控件状态
        setupEditText()
        when (mode) {

            MODE_DEGREE -> {

                degreeLayout.visibility = View.VISIBLE

                pointLayout.visibility = View.GONE

                secondLayout.visibility = View.GONE

            }

            MODE_DEGREE_POINT -> {

                degreeLayout.visibility = View.VISIBLE

                pointLayout.visibility = View.VISIBLE

                secondLayout.visibility = View.GONE

            }

            MODE_DEGREE_POINT_SECOND -> {

                degreeLayout.visibility = View.VISIBLE

                pointLayout.visibility = View.VISIBLE

                secondLayout.visibility = View.VISIBLE

            }

        }

    }

    private fun setupEditText() {

        // 设置输入框监听
        degreeEditText.setOnFocusChangeListener { _, hasFocus ->

            if (!hasFocus) {
                onDegreeChangeListener?.invoke(degreeEditText.text.toString())
            }
        }
        pointEditText.setOnFocusChangeListener { _, hasFocus ->

            if (!hasFocus) {
                onPointChangeListener?.invoke(pointEditText.text.toString())

            }

        }
        secondEditText.setOnFocusChangeListener { _, hasFocus ->

            if (!hasFocus) {
                onSecondChangeListener?.invoke(secondEditText.text.toString())

            }

        }

    }


    fun getDegreeEditValue(): String {
        return degreeEditText.text.toString()
    }

    fun getPointEditValue(): String {
        return pointEditText.text.toString()
    }

    fun getSecondEditValue(): String {
        return secondEditText.text.toString()
    }


}